import CryptoJS from "crypto-js"

const key = CryptoJS.enc.Utf8.parse("0123456789abcdef");  //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('0123456789abcdef');   //十六位十六进制数作为密钥偏移量

function encrypt(content) {
    var str = content.toString();
    var encrypted = CryptoJS.AES.encrypt(str, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });

    // 转换为字符串
    encrypted = encrypted.toString();

    return encrypted;
}

function decrypt(content) {
    content = decodeURIComponent(content)
    // DES 解密
    var decrypted = CryptoJS.AES.decrypt(content, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    
    // 转换为 utf8 字符串
    decrypted = CryptoJS.enc.Utf8.stringify(decrypted);
    return decrypted;
}

export {
    encrypt,
    decrypt
}